//
//  YSPlugin.m
//  HelloCordova
//
//  Created by 6CIT－APPLE on 15/11/1.
//
//

#import "YSPlugin.h"
#import "RealPlayViewController.h"
#import "YSPlayerController.h"
#import "YSHTTPClient.h"
#import "YSMobilePages.h"

#define AppKey        @"267f67f7b3f544578d1f6b1b0bdfa2a4"

@interface YSPlugin()

@property (nonatomic, strong) RealPlayViewController* realPlayController;

//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC;

@end

@implementation YSPlugin

- (RealPlayViewController*)realPlayController {
    if (_realPlayController == nil) {
        _realPlayController = [[RealPlayViewController alloc]init];
    }
    
    return _realPlayController;
}


- (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

- (void)initSDK:(CDVInvokedUrlCommand *)command {
    CDVPluginResult* pluginResult = nil;
    
    NSMutableDictionary *dictServers = [NSMutableDictionary dictionary];
    [dictServers setObject:@"https://auth.ys7.com" forKey:kAuthServer];
    [dictServers setObject:@"https://open.ys7.com" forKey:kApiServer];
    [YSPlayerController loadSDKWithPlatfromServers:dictServers];
    
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)login:(CDVInvokedUrlCommand *)command {
    
    [[YSHTTPClient sharedInstance] setClientAppKey:AppKey];
    
    YSMobilePages* pages = [[YSMobilePages alloc]init];
    
    [pages login:[self getCurrentVC] completion:^(NSString *accessToken) {
        CDVPluginResult* pluginResult = nil;
        if (accessToken != nil && [accessToken length] > 0) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:accessToken];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
    }];
  
}

- (void)realPlay:(CDVInvokedUrlCommand*)command {
    __block CDVPluginResult* pluginResult = nil;
    
    NSString* accessToken = [command.arguments objectAtIndex:0];
    NSString* cameraId = [command.arguments objectAtIndex:1];
    
    if (accessToken != nil && accessToken.length > 0 &&
        cameraId != nil && cameraId.length > 0) {
        
        self.realPlayController.accessToken = accessToken;
        self.realPlayController.cameraId = cameraId;
        
        [[self getCurrentVC] presentViewController:self.realPlayController animated:NO completion:^{
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }];
        
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
    
}

@end
